#include "stdafx.h"
#include <stdio.h>
#include <string.h>


// lua.hpp
// Lua header files for C++
// <<extern "C">> not supplied automatically because Lua also compiles as C++

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}


static void stackDump (lua_State *L) {
	int i;
	int top = lua_gettop(L);
	for (i = 1; i <= top; i++) {  /* repeat for each level */
		int t = lua_type(L, i);
		switch (t) {

	   case LUA_TSTRING:  /* strings */
		   printf("`%s'", lua_tostring(L, i));
		   break;

	   case LUA_TBOOLEAN:  /* booleans */
		   printf(lua_toboolean(L, i) ? "true" : "false");
		   break;

	   case LUA_TNUMBER:  /* numbers */
		   printf("%g", lua_tonumber(L, i));
		   break;

	   default:  /* other values */
		   printf("%s", lua_typename(L, t));
		   break;

		}
		printf("  ");  /* put a separator */
	}
	printf("\n");     /* end the listing */
}

#define MAX_COLOR       255

/* assume that table is on the stack top */
int getfield (const char *key,lua_State *L) {
	int result= 0;
	lua_pushstring(L, key);
	stackDump(L);
	lua_gettable(L, -2);  /* get background[key] */
	if (!lua_isnumber(L, -1))
		printf("error");
		//error(L, "invalid component in background color");
	//result = (int)lua_tonumber(L, -1) * MAX_COLOR;
	float temp= float(lua_tonumber(L, -1));
	lua_pop(L, 1);  /* remove number */
	return result;
}

//void setcolor (struct ColorTable *ct,,lua_State *L) {
//	lua_newtable(L);            /* creates a table */
//	setfield("r", ct->red);     /* table.r = ct->r */
//	setfield("g", ct->green);   /* table.g = ct->g */
//	setfield("b", ct->blue);    /* table.b = ct->b */
//	lua_setglobal(ct->name);    /* 'name' = table */
//}

int luaTest ()
{
	//char buff[256];
	//int error;
	lua_State *L = lua_open();  /* opens Lua */
	int nRes = luaL_dofile(L,"test1.lua");
	lua_getglobal(L, "background");
	stackDump(L);
	int red = getfield("r",L);
	int green = getfield("g",L);
	int blue = getfield("b",L);
	//lua_pushboolean(L, 1); lua_pushnumber(L, 10);
	//lua_pushnil(L); lua_pushstring(L, "hello");
	//stackDump(L);
	///* true  10  nil  `hello'  */

	//lua_pushvalue(L, -4); stackDump(L);
	///* true  10  nil  `hello'  true  */

	//lua_replace(L, 3); stackDump(L);
	///* true  10  true  `hello'  */

	//lua_settop(L, 6); stackDump(L);
	///* true  10  true  `hello'  nil  nil  */

	//lua_remove(L, -3); stackDump(L);
	///* true  10  true  nil  nil  */

	//lua_settop(L, -5); stackDump(L);
	///* true  */


	//luaopen_base(L);         /* opens the basic library */
	//luaopen_table(L);        /* opens the table library */
	////luaopen_io(L);           /* opens the I/O library */
	//luaopen_string(L);       /* opens the string lib. */
	//luaopen_math(L);         /* opens the math lib. */
//
	//while (fgets(buff, sizeof(buff), stdin) != NULL) {
	//	error = luaL_loadbuffer(L, buff, strlen(buff),
	//		"line") || lua_pcall(L, 0, 0, 0);
	//	if (error) {
	//		fprintf(stderr, "%s", lua_tostring(L, -1));
	//		lua_pop(L, 1);/* pop error message from the stack */
	//	}
	//}
	//lua_pushinteger(L,111);
	//lua_pushinteger(L,222);
	//lua_pushstring(L,"haha");
	//stackDump(L);

	//int test = lua_tonumber(L,1);
	//const char* pchar = lua_tostring(L,-1);

	//int test2 = lua_tonumber(L,1);

	lua_close(L);
	return 0;
}